// 将文件blob转换为string
export const blobToString: (file: File) => Promise<string> = (file: File) => {
  return new Promise((resolve) => {
    const reader = new FileReader()
    reader.readAsText(file, 'UTF-8')
    reader.onload = function (evt) {
      const fileString = evt?.target?.result || ''
      if (typeof fileString === 'string') {
        resolve(fileString)
      }
    }
  })
}

// 下载excel
export const downloadExcel = (res: any) => {
  const fileName = res.headers['content-disposition']?.split('filename=')[1] || 'newFile.xlsx'
  const blob = new Blob([res.data])
  const elink = document.createElement('a')
  elink.href = URL.createObjectURL(blob)
  elink.download = fileName
  elink.click()
}

export default {
  blobToString,
  downloadExcel
}
